﻿@model HWmanagementClient.Models.TeacherHomeworks
@{
    ViewBag.Title = "Index";
}
<script type="text/javascript">
    $(function () {
        $("#studentcourses").accordion();
        $("#newhw_deadline3").datepicker({
            numberOfMonths: 3,
            showButtonPanel: true
        });
    });

    function newhw() {
        $("input[type=submit]").button();
        $("#dialog-form").dialog({
            modal: true,
            height: 600,
            width: 800,
            buttons: {
                Mégse: function () {
                    $(this).dialog("close");
                }
            }
        });
    }

    var markup2 = '<div id="trashable" class="todel${Neptun}">' +
                  '<strong>${Name}</strong> (${Neptun})' +
                  '<a href="#" class="buttonclass" style="color: white;" onclick="delfromgroup(\'${Neptun}\');">Törlés</a>' +
                  '</div>';

    function delfromgroup(user) {
        $.ajax({
            type: "POST",

            url: "/Teacher/DelFromGroup/",
            data: { user: user, gid: $('#groupid').val() },

            async: false
        })
			.done(function (msg) {
			    if (msg == true) {
			        $(".todel" + user).hide();
			    }
			    else {
			        alert("Hiba történt a törlés közben");
			    }
			});
        
    }

    function addtogroup() {
        var usr = $('#togroup').val() == "" ? "no" : $('#togroup').val();
        $.ajax({
            type: "POST",

            url: "/Teacher/AddToGroup/",
            data: { user: usr, gid: $('#groupid').val(), hid: $('#hwid').val() },

            async: false
        })
			.done(function (msg) {
			    if (msg == true) {
			        //alert("OK");
			        
			        var neptun = usr.substring(usr.indexOf('(') + 1, usr.indexOf(')'));
			        var name = usr.substring(0, usr.indexOf('('));
			        hw = { Name: name, Neptun: neptun };
			        $.template("u_templ", markup2);
			        $("#usrcontainer").before($.tmpl("u_templ", hw));
			        //$.tmpl("hw_templ", hw).appendTo("#hwcontainer");
			        $("a.buttonclass").button();
			    }
			    else {
			        alert("Hiba történt a hozzáadás közben");
			    }
			});
    }

    function delhw(id, cid) {
        showLoader();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Teacher/DeleteHomework/" + id,
            dataType: "json",
            async: false
        })
			.done(function (msg) {
			    if (msg == true) {
			        location.reload();
			    }
			    else {
			        alert("Hiba történt a törlés közben");
			    }
			    hideLoader();
			});
    }

    

    function grouping(id, gid) {
        $("#groupingHW").show("slow");
        $("#addnewHW").hide("slow");
        $("#groupid").val(gid);
        $("#hwid").val(id);
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Teacher/GetStudents/"+id,
            dataType: "json",
            async: false
        })
			.done(function (msg) {
			    $('#togroup').autocomplete({
			        source: msg
			    });
			});


        showLoader();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Teacher/GetGroupUsers/" + id,
            dataType: "json",
            async: false
        })
			.done(function (msg) {
			    var hw = msg.users;
			    //alert(hw);
			    if (hw != null) {
			        $("div#trashable").remove();
			        $.template("u_templ", markup2);
			        $("#usrcontainer").before($.tmpl("u_templ", hw));
			        //$.tmpl("hw_templ", hw).appendTo("#hwcontainer");
			        $("a.buttonclass").button();
			    }
			    hideLoader();
			});
    }

    function ungrouping() {
        $("#groupingHW").hide("slow");
        $("#addnewHW").show("slow");
        $("#courseid").val(0);
        $("#hwid").val(0);
    }

    var markup = '<tr id="trashable">' +
                        '<td><strong>${title}</strong> (ID: ${id}, C: ${courseid}, U: ${userid})' +
                        '<br>${shortdesc}<br>Csatolt fájl: <a target="_blank" href="${fileurl}">${filename}</a><br>' +
                        '<em>Beadási határidő: <strong>${deadline2}</strong></em></td>' +
                        '<td>' +
                        '<a href="#" class="buttonclass" style="color: white;" onclick="grouping(${id},${groupid});">Felhasználók megadása</a>' +
                        '</td>' +
                        '<td>' +
                        '<a href="#" class="buttonclass" style="color: white;" onclick="delhw(${id},${courseid});">Törlés</a>' +
                        '</td>' +
                    '</tr>';



    function getHomework(id) {
        $("#newh").show();
        showLoader();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Teacher/GetHomeworks/" + id,
            dataType: "json",
            async: false
        })
			.done(function (msg) {
			    var hw = msg.Homeworks;
			    //alert(hw);
			    $("tr#trashable").remove();
			    $.template("hw_templ", markup);
			    $("#hwcontainer").before($.tmpl("hw_templ", hw));
			    //$.tmpl("hw_templ", hw).appendTo("#hwcontainer");
			    $("a.buttonclass").button();
			    $("#couid").val(id);
			    hideLoader();
			});
    }

</script>
<script id="hw_templ" type="text/x-jQuery-tmpl">
            
</script>
<h2>
    Új házi feladat</h2>
@if (ViewBag.Succeeded == true)
{
    <div class="ui-widget">
        <div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;">
            <p>
                <span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
                <strong>OK!</strong> A feladat sikeresen kiírva.</p>
        </div>
    </div>
    <br />
    <br />
}
else if (ViewBag.Succeeded == false)
{
    <div class="ui-widget">
        <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
            <p>
                <span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
                <strong>HIBA!</strong> A kiírás nem sikerült.</p>
        </div>
    </div>
    <br />
    <br />
}
<div id="addnewHW">
    <table>
        <tr>
            <td valign="top" style="width: 300px;">
                <div id="studentcourses">
                    @foreach (var item in Model.Subjects)
                    {
                        <h3>
                            @item.name (@item.neptun.ToUpper())</h3>
                        <div>
                            @foreach (var course in item.courses)
                            {
                                <a href="#" onclick="getHomework(@course.id);">[ @course.semester ]</a> 
                            }
                        </div>
                    }
                </div>
            </td>
            <td>
                <table>
                    <tbody>
                        <tr>
                            <td colspan="2">
                                <a href="#" id="newh" class="buttonclass" style="color: white; display: none;" onclick="newhw();">
                                    Új házi feladat kiírása</a>
                            </td>
                        </tr>
                        <tr id="hwcontainer">
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
</div>
<div id="groupingHW" style="display: none;">
    <input type="hidden" id="groupid" name="gid" />
    <input type="hidden" id="hwid" name="hid" />
    <input type="text" id="togroup" />
    <a href="#" id="add" class="buttonclass" style="color: white;" onclick="addtogroup();">
        Hozzáadás</a> <a href="#" id="end" class="buttonclass" style="color: white;" onclick="ungrouping();">
            Befejezés</a>
    <br />
    <br />
    <div id="usrcontainer">
    </div>
</div>
<div id="dialog-form" title="Házi feladat beadás" style="display: none;">
    @using (Html.BeginForm("Index", "Teacher", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div>
            <fieldset>
                <legend>Új feladat kiírása</legend>
                <input type="hidden" name="courseid" value="none" id="couid" />
                <div class="editor-label">
                    Feladat címe*:
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.newhw.title)
                </div>
                <div class="editor-label">
                    Rövid leírás*:
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.newhw.shortdesc)
                </div>
                <div class="editor-label">
                    Beadási határidő*:
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.newhw.deadline3)
                </div>
                <div class="editor-label">
                    Csatolt fájl:
                </div>
                <div class="editor-field">
                    <input type="file" name="file" />
                </div>
                <br />
                <br />
                <br />
                <br />
                <br />
                <input type="submit" value="OK" />
            </fieldset>
        </div>
        
    }
</div>
